<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <title></title>
    <script th:src="@{/layer/JQuery/jquery-3.1.1.min.js}"></script>
    <script th:src="@{/layer/layer/layer.js}"></script>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}"/>
    <script th:src="@{/layui/layui.js}"></script>
    <style>
        #uploadfile {
            margin: 10px;
            width: 200px;
        }

        .layui-input-block {
            margin-right: 200px;
        }
    </style>
</head>

<body>
<form class="layui-form" th:action="@{/file}" method="post" enctype="multipart/form-data">

    <div class="layui-form-item" style="margin-top: 10px">
        <label class="layui-form-label">文件名称</label>
        <div class="layui-input-block">
            <input type="text" id="fname" name="fname" autocomplete="off"
                   class="layui-input" maxlength="20" placeholder="此项为非必填项，若没有填写，默认问上传的文件名称">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">所属目录</label>
        <div class="layui-input-block">
            <select name="fcid" lay-verify="required">
                <option></option>
                <option th:each="catalog:${catalogs}" th:text="${catalog.cname}"
                        th:value="${catalog.cid}"></option>
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label" style="color: red">上传文件</label>
        <div class="layui-input-block">
            <input type="file" id="uploadfile" name="uploadfile" lay-verify="required" autocomplete="off">
            <p style="color: blue" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
            <span id="ts">注意！文件最大为20MB</span>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" id="addbutton" lay-submit="" lay-filter="demo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <a th:href="@{/catalogs}">
                <input type="button" class="layui-btn layui-btn-primary" value="返回" style="margin-left: 9px;"></input>
            </a>
        </div>
    </div>

</form>
<script>
    layui.use('form', function () {
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function (data) {
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });

    $("#fname").blur(function () {
        var temp = $("#fname").val();
        if(temp.replace(/[ ]/g, '')==''){
            $("#fname").val("")
        }
    });
</script>
</body>

</html>